package com.easidea.tim.flex.remote.finance
{
	import com.easidea.tim.flex.Env;
	import com.easidea.tim.flex.bean.Page;
	import com.easidea.tim.flex.bean.finance.DueFromPayedRecord;
	import com.easidea.tim.flex.event.CustomerEvent;
	import com.easidea.tim.flex.remote.BaseDao;
	import com.easidea.tim.flex.util.AppAlert;
	
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	import net.digitalprimates.persistence.hibernate.rpc.HibernateRemoteObject;

	public class DueFromPayedRecordDao extends BaseDao
	{
		private var _dispatch:String;
		private var _DueFromPayedRecordService:HibernateRemoteObject = new HibernateRemoteObject();
		private static var _instance:DueFromPayedRecordDao;
		
		public static function getInstance():DueFromPayedRecordDao{
			if( !_instance ){
				_instance = new DueFromPayedRecordDao();
				
			}
			return _instance;
		}
		public function DueFromPayedRecordDao() {
				_DueFromPayedRecordService.channelSet = this.buildChannelSet();
				_DueFromPayedRecordService.destination="dueFromPayedRecordService";	
				_DueFromPayedRecordService.concurrency = "last";
				_DueFromPayedRecordService.makeObjectsBindable = true;	
				_DueFromPayedRecordService.showBusyCursor = true;
		}
		/**
		 * 调用spring方法:根据查询条件分页查询采购支付列表
		 * @param name
		 * @param page
		 * 
		 */		
		public function getDueFromPayedRecordListByCondition(customerId:int,customerName:String, 
					startDate:Date,endDate:Date,
					orderId:int, page:Page,dispatch:String=null):void {	
			_dispatch = dispatch;
			_DueFromPayedRecordService.addEventListener(ResultEvent.RESULT, getDueFromPayedRecordListByConditionResult);
			_DueFromPayedRecordService.addEventListener(FaultEvent.FAULT,_Fault);
			_DueFromPayedRecordService.getDueFromPayedRecordListByCondition(customerId,customerName,startDate,endDate,orderId, page);
		}

		/**
		 * 根据查询条件分页查询采购支付列表回调函数
		 * @param e
		 * 
		 */			
		private function getDueFromPayedRecordListByConditionResult(e:ResultEvent):void {		
			_DueFromPayedRecordService.removeEventListener(ResultEvent.RESULT, getDueFromPayedRecordListByConditionResult);
			if(_dispatch != null && _dispatch =="viewDetailByCustomerId") {
				Env.dispatchEvent(new CustomerEvent("viewDetailByCustomerIdSuccess",e.result));
			}else {
				Env.dispatchEvent(new CustomerEvent("getDueFromPayedRecordListByConditionSuccess",e.result));
			}
								  									 	
		}
		
		/**
		 * 调用spring方法:获取采购订单流水号信息
		 * @param name
		 * @param page
		 * 
		 */		
		public function getAllSaleOrderList():void {			
			_DueFromPayedRecordService.addEventListener(ResultEvent.RESULT, getAllSaleOrderListResult);
			_DueFromPayedRecordService.addEventListener(FaultEvent.FAULT, _Fault);
			_DueFromPayedRecordService.getAllSaleOrderList();
		}
		/**
		 * 获取采购订单流水号信息回调函数
		 * @param e
		 * 
		 */			
		private function getAllSaleOrderListResult(e:ResultEvent):void {			 
			_DueFromPayedRecordService.removeEventListener(ResultEvent.RESULT, getAllSaleOrderListResult);

			 if(e.result != null) {					 				 
				Env.dispatchEvent(new CustomerEvent("getAllSaleOrderListSuccess",e.result));				  									 
			 } 
		}
		
		/**
		 * 调用spring方法:根据输入字符过滤客户
		 * @param searchText
		 * 
		 */		
		public function filterCustomerByName(searchText:String):void {			
			_DueFromPayedRecordService.addEventListener(ResultEvent.RESULT, filterCustomerByNameResult);
			_DueFromPayedRecordService.addEventListener(FaultEvent.FAULT, _Fault);
			_DueFromPayedRecordService.filterCustomerByName(searchText);
		}
		/**
		 *根据输入字符过滤客户回调函数
		 * @param e
		 * 
		 */			
		private function filterCustomerByNameResult(e:ResultEvent):void {			 
			_DueFromPayedRecordService.removeEventListener(ResultEvent.RESULT, filterCustomerByNameResult);

			 if(e.result != null) {					 				 
				Env.dispatchEvent(new CustomerEvent("filteCustomerSuccess",e.result));				  									 
			 } 
		}	
		/**
		 * 调用spring方法:根据输入字符过滤销售员
		 * @param searchText
		 * 
		 */		
		public function getStaffListbyName(searchText:String):void {			
			_DueFromPayedRecordService.addEventListener(ResultEvent.RESULT, getStaffListbyNameResult);
			_DueFromPayedRecordService.addEventListener(FaultEvent.FAULT, _Fault);
			_DueFromPayedRecordService.getStaffListbyName(searchText);
		}
		/**
		 * 根据输入字符过滤销售员回调函数
		 * @param e
		 * 
		 */			
		private function getStaffListbyNameResult(e:ResultEvent):void {			 
			_DueFromPayedRecordService.removeEventListener(ResultEvent.RESULT, getStaffListbyNameResult);

			 if(e.result != null) {					 				 
				Env.dispatchEvent(new CustomerEvent("filteStaffSuccess",e.result));				  									 
			 } 
		}	
		/**
		 *调用spring方法:按客户统计 
		 * @param customerName
		 * @param responer
		 * @param startDate
		 * @param endDate
		 * @param page
		 * 
		 */		
		public function getDueFromSummarByCustomer(customerId:int,responer:String,startDate:Date,endDate:Date,page:Page):void {
			_DueFromPayedRecordService.addEventListener(ResultEvent.RESULT, getDueFromSummarByCustomerResult);
			_DueFromPayedRecordService.addEventListener(FaultEvent.FAULT, _Fault);
			_DueFromPayedRecordService.getDueFromSummarByCustomer(customerId,responer,startDate,endDate,page);			
		}
		/**
		 * 按客户统计回调函数
		 * @param e
		 * 
		 */		
		private function getDueFromSummarByCustomerResult(e:ResultEvent):void {
			_DueFromPayedRecordService.removeEventListener(ResultEvent.RESULT, getDueFromSummarByCustomerResult);

			 if(e.result != null) {					 				 
				Env.dispatchEvent(new CustomerEvent("getDueFromSummarByCustomerSuccess",e.result));				  									 
			 } 			
		} 
		/**
		 *调用spring方法:按销售员统计 
		 * @param customerName
		 * @param responer
		 * @param startDate
		 * @param endDate
		 * @param page
		 * 
		 */		
		public function getDueFromSummarByStaff(username:String,startDate:Date,endDate:Date,page:Page):void {
			_DueFromPayedRecordService.addEventListener(ResultEvent.RESULT, getDueFromSummarByStaffResult);
			_DueFromPayedRecordService.addEventListener(FaultEvent.FAULT, _Fault);
			_DueFromPayedRecordService.getDueFromSummarBySalesStaff(username,startDate,endDate,page);			
		}
		/**
		 * 按销售员统计回调函数
		 * @param e
		 * 
		 */		
		private function getDueFromSummarByStaffResult(e:ResultEvent):void {
			_DueFromPayedRecordService.removeEventListener(ResultEvent.RESULT, getDueFromSummarByStaffResult);

			 if(e.result != null) {					 				 
				Env.dispatchEvent(new CustomerEvent("getDueFromSummarByStaffSuccess",e.result));				  									 
			 } 			
		} 		
		/**
		 * 调用spring方法:保存采购订单信息
		 * @param name
		 * @param page
		 * 
		 */		
		public function savePay(pay:DueFromPayedRecord):void {			
			_DueFromPayedRecordService.addEventListener(ResultEvent.RESULT, savePayResult);
			_DueFromPayedRecordService.addEventListener(FaultEvent.FAULT, _Fault);
			_DueFromPayedRecordService.savePay(pay);
		}
		/**
		 * 保存采购订单信息回调函数
		 * @param e
		 * 
		 */			
		private function savePayResult(e:ResultEvent):void {			 
			_DueFromPayedRecordService.removeEventListener(ResultEvent.RESULT, savePayResult);

			 if(e.result != null) {	
			 	if(e.result.toString() == "success") {
			 	 AppAlert.show("保存成功！","信息提示");
			 	 Env.dispatchEvent(new CustomerEvent("savePaySuccess"));	
			 	}				 				 
							  									 
			 } 
		}							
	}
}